'----------------------------------------------
' Name: Ali Karbassi & Matt Latzke
' Date: March 20, 2007
' Description: Project 4
'              Form frmAll
'----------------------------------------------

Imports System.IO

Public Class frmAll

   Friend dbkey As Microsoft.Win32.RegistryKey
   Friend dblocation As String
   Friend connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="

   Const configFileName As String = "prog4.ini"
   Dim configFileLocation As String

   ''' <summary>
   ''' Exits the program
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
   Handles btnExit.Click

      Application.Exit()

   End Sub

   ''' <summary>
   ''' Saves the database
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub EmployeeBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As _
   System.EventArgs)

      Me.Validate()
      Me.EmployeeBindingSource.EndEdit()
      Me.EmployeeTableAdapter.Update(Me.ActivityDataSet.Employee)

   End Sub

   ''' <summary>
   ''' Attempts to load a database.  If there is no database at the location coded,
   ''' an open dialog is opened.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub frmAll_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
   Handles MyBase.Load

      Dim connected As Boolean = False

      findBin()

      Try

         getDBLocation()
         EmployeeTableAdapter.Connection.ConnectionString = connString & dblocation

      Catch ex As Exception

         openDialog()
         EmployeeTableAdapter.Connection.ConnectionString = connString & dblocation

      End Try


      While connected = False

         Try

            Me.EmployeeTableAdapter.Fill(Me.ActivityDataSet.Employee)
            connected = True

         Catch ex As Exception

            openDialog()
            EmployeeTableAdapter.Connection.ConnectionString = connString & dblocation

         End Try


      End While

      saveDBLocation(dblocation)

   End Sub

   ''' <summary>
   ''' Hides this form and shows Form Individual
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub btnIndividual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
   btnIndividual.Click

      Me.Hide()
      frmIndividual.Show()

   End Sub

   ''' <summary>
   ''' Refreshes the tables
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub btnReload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
   btnReload.Click

      EmployeeDataGridView.Refresh()

   End Sub

   ''' <summary>
   ''' Finds the config file
   ''' </summary>
   ''' <remarks></remarks>
   Private Sub findBin()

      Dim binLocation As String = ""
      Dim currentLocation As Array = Split(My.Computer.FileSystem.CurrentDirectory, "\")

      Dim x As Integer = 0

      While (currentLocation.Length >= x And Not currentLocation(x).ToString.Equals("bin"))

         binLocation &= currentLocation(x).ToString & "/"
         x += 1

      End While

      binLocation &= currentLocation(x).ToString
      configFileLocation = binLocation & "/" & configFileName

   End Sub

   ''' <summary>
   ''' Gets the database location
   ''' </summary>
   ''' <remarks></remarks>
   Private Sub getDBLocation()

      Dim text As String()
      text = File.ReadAllLines(configFileLocation)

      Dim x As Integer = 0

      While (text.Length > x)

         If text(x).Contains("Location|") Then

            dblocation = Split(text(x), "|")(1).ToString

         End If

         x += 1

      End While

   End Sub

   ''' <summary>
   ''' Saves the database location
   ''' </summary>
   ''' <param name="DataBaseLocation"></param>
   ''' <remarks></remarks>
   Private Sub saveDBLocation(ByVal DataBaseLocation As String)

      dblocation = DataBaseLocation

      Dim outputFile As StreamWriter

      If Not File.Exists(configFileLocation) Then
         File.Delete(configFileLocation)
      End If

      outputFile = File.CreateText(configFileLocation)
      outputFile.WriteLine("Location|" & dblocation)
      outputFile.Close()

   End Sub

   ''' <summary>
   ''' Opens the file dialog.
   ''' </summary>
   ''' <remarks></remarks>
   Private Sub openDialog()

      Dim openDB As New OpenFileDialog
      openDB.Filter = "MS Access Database (*.mdb)|*.mdb"

      If openDB.ShowDialog() = Windows.Forms.DialogResult.OK Then

         dblocation = openDB.FileName

      Else

         MsgBox("No Database Selected!")
         End
         Application.Exit()

      End If

   End Sub

End Class